#!/bin/sh

#
# This file is released under the terms of the Artistic License.
# Please see the file LICENSE, included in this package, for details.
#
# Copyright (C) 2002-2006 Rod Taylor & Open Source Development Labs, Inc.
#               2010 Mark Wong
#

LOGFILE="log"
OUTDIR="."
while getopts "fo:p:" OPT; do
	case ${OPT} in
	f)
		rm -f ${DBT5PGDATA}/postmaster.pid
		;;
	o)
		OUTDIR=${OPTARG}
		;;
	p)
		PARAMETERS=$OPTARG
		;;
	esac
done

if [ -z ${DBT5PGDATA} ]; then
	echo "DBT5PGDATA not defined."
	exit 1
fi

if [ -f ${DBT5PGDATA}/postmaster.pid ]; then
	echo "Database is already started."
	exit 0
fi

# We need the sleeps just in case we start pg_ctl commands too closely
# together.  Only start pg_autovacuum if explicitly called.

sleep 1

if [ "x${PARAMETERS}" = "x" ]; then
	pg_ctl -w -D ${DBT5PGDATA} -l ${OUTDIR}/${LOGFILE} start
else
	pg_ctl -w -D ${DBT5PGDATA} -o "${PARAMETERS}" -l ${OUTDIR}/${LOGFILE} start
fi

if [ ! -f ${DBT5PGDATA}/postmaster.pid ]; then
	echo "database did not start correctly, check database log"
	exit 1
fi

exit 0
